import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter_echarts/flutter_echarts.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/resources/str_res_keys.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年03月06日 18:00
 */
class CurrentVoltageChart extends StatelessWidget {
  List<dynamic> maxVoltage;//最高允许充电电压
  List<dynamic> needVoltage;//需求电压
  List<dynamic> outVoltage;//充电输出电压
  List<dynamic> bmsVoltage;//BMS测量电压
  List<dynamic> needCurrent;//需求电流
  List<dynamic> maxCurrent;//最高允许充电电流
  List<dynamic> outCurrent;//充电输出电流
  List<dynamic> bmsCurrent;//BMS测量电流
  CurrentVoltageChart({
    super.key,
    required this.maxVoltage,
    required this.needVoltage,
    required this.outVoltage,
    required this.bmsVoltage,
    required this.needCurrent,
    required this.maxCurrent,
    required this.outCurrent,
    required this.bmsCurrent,
  });
  List<String> paramList = [
    ResKey.maxAllowableChargingVoltage.tr,
    ResKey.needVoltage.tr,
    ResKey.outVoltage.tr,
    ResKey.bmsVoltage.tr,
    ResKey.maxAllowableChargingCurrent.tr,
    ResKey.needCurrent.tr,
    ResKey.outCurrent.tr,
    ResKey.bmsCurrent.tr,
  ];
  @override
  Widget build(BuildContext context) {
    // Log.d('bmsCurrent===' + bmsCurrent.toString());
    return Echarts(
      captureHorizontalGestures: true,
      option: '''
 {
  color: ['#3DD598', '#007DFF'],
  /// 横轴
  xAxis: {
    type: 'category',                
    axisLine:{
      show: false,
    }, 
    
    axisTick: {
      show: false
    }, 
    axisLabel: {
      color: '#999999',
      fontSize: 11,
      interval: function (index) { // 自定义间隔函数 
          if (index % 240 === 0) { // 每四小时显示一次  // 返回值是五分钟为一个间隔
              return true;
          } else {
              return false;
          }
      },
    },
  },
  yAxis: [
  
    {
      type: 'value',
      name:  ${jsonEncode(ResKey.gunVoltage.tr)},
      alignTicks: true,
      nameTextStyle: {
        align: "center"
      },
      align: 'left',
      padding: [3, 4, 5, 6],
      verticalAlign: 'top',
      nameLocation: 'end',
      position: 'left',
      splitLine: {
        lineStyle: {
          type: 'dashed', //虚线
        },
      },
    },
    {
      type: 'value',
      name:  ${jsonEncode(ResKey.gunCurrent.tr)},
       nameTextStyle: {
        align: "center"
      },
      nameLocation: 'end',
      splitLine: {
        lineStyle: {
          type: 'dashed', //虚线
        },
      },
    },
  ],
  grid: {
    top: 30,
    bottom: 90,
    right: 40,
    left: 40,
    containLabel: true,
  },
  legend: {
    show: true,
    icon: 'rect',
        left:20,
    right:20,
    top:'280',
        itemHeight: 3,
    itemWidth: 12,
     textStyle: {
      borderRadius: [2, 2, 2, 2]
    },
    padding: [
    0,  // 上
    0, // 右
    0,  // 下
    0, // 左
]
  },
  tooltip: {
    trigger: 'axis',
    backgroundColor: 'rgba(0,0,0,0.85)',
    borderWidth: 0,
    opacity: 0.2,
    confine: true,
    padding: [
      10, // 上
      8, // 右
      10, // 下
      8 // 左
    ],
    textStyle: {
      color: 'white'
    },
  },
  dataZoom: [
    {
      type: 'inside',
      realtime: false,
    },
    // {
    //   start: 0,
    //   end: 100,
    //   height: 15,
    //   realtime: false,
    // },
  ],
  series: [
    {
      name: ${jsonEncode(paramList[0])},
      color: '#1890FF',
      type: 'line',
      yAxisIndex: 0,
      data: ${jsonEncode(maxVoltage)},
    },
    {
      name: ${jsonEncode(paramList[1])},
      color: '#2FC25B',
      type: 'line',
      yAxisIndex: 0,
      data: ${jsonEncode(needVoltage)},
    },
    {
      name: ${jsonEncode(paramList[2])},
      color: '#5D7092',
      type: 'line',
      yAxisIndex: 0,
      data: ${jsonEncode(outVoltage)},
    },
    {
      name: ${jsonEncode(paramList[3])},
      color: '#FAC958',
      type: 'line',
      yAxisIndex: 0,
      data: ${jsonEncode(bmsVoltage)},
    },
    {
      name: ${jsonEncode(paramList[4])},
      color: '#67D7FC',
      type: 'line',
      yAxisIndex: 1,
      data: ${jsonEncode(needCurrent)},
    },
    {
      name: ${jsonEncode(paramList[5])},
      color: '#FF7070',
      type: 'line',
      yAxisIndex: 1,
       data: ${jsonEncode(maxCurrent)},
    },
    {
      name: ${jsonEncode(paramList[6])},
      color: '#8080FF',
      type: 'line',
      yAxisIndex: 1,
      data: ${jsonEncode(outCurrent)},
    },
    {
      name: ${jsonEncode(paramList[7])},
      color: '#F79631',
      type: 'line',
      yAxisIndex: 1,
      data: ${jsonEncode(bmsCurrent)},
    },
  ],
}
  ''',
    );
  }
}
